/*
 * test.java
 *
 * 
 */
package it.luigiarpino.alt2jsim.netpanel.flat;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Rectangle;
import java.lang.Math;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;



/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class CirclePanel extends JPanel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = -4528542519045309378L;
	private int[][] link;
    private int numNode;
    private int selectedLink=-1;
    private boolean[] redNode;
    private String[] nomiNodi;
    private BufferedImage bufferedImageNodoRosso;
    private BufferedImage bufferedImageNodoBlu;


    /**Mostra nodi come reti di terzo livello*/
    public static final int LEVEL2NET=2;
    /** Mostra nodi come reti di secondo livello */
    public static final int LEVEL1NET=1;
    /**Mostra nodi router*/
    public static final int ROUTER=3;
    /**Mostra nodi transit*/
    public static final int TRANSIT_NODE=4;
    /**Mostra nodi come reti transit*/
    public static final int TRANSIT_NET=5;
    
    
    /** Creates a new instance of test */
    public CirclePanel(int[][] link_, int numNode_, String[] nomiNodi_, int image ) {
        super();
        try { 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());     
    	} 
    	catch(Exception e) { }
        this.nomiNodi=nomiNodi_;
        this.link=link_;
        this.numNode=numNode_;
        this.redNode=new boolean[this.numNode];
        for (int i=0;i<this.numNode;i++){
            this.redNode[i]=false;
        }
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mouseClick(evt);
            }
        });
        try{
            //carica i nodi come reti di secondo livello
            if(image==CirclePanel.LEVEL1NET){
                java.net.URL u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/level2netBlu.gif");
                this.bufferedImageNodoBlu= ImageIO.read(u);
                u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/level2netRosso.gif");
                this.bufferedImageNodoRosso= ImageIO.read(u);
            }
            //carica i nodi come reti di terzo livello
            if(image==CirclePanel.LEVEL2NET){
                java.net.URL u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/level3netBlu.gif");
                this.bufferedImageNodoBlu= ImageIO.read(u);
                u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/level3netRosso.gif");
                this.bufferedImageNodoRosso= ImageIO.read(u);
            }
            //carica i nodi come router
            if (image==CirclePanel.ROUTER){
                java.net.URL u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/routerBlu.gif");
                this.bufferedImageNodoBlu= ImageIO.read(u);
                u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/routerRosso.gif");
                this.bufferedImageNodoRosso= ImageIO.read(u);
            }
            //carica i nodi come reti transit
            if (image==CirclePanel.TRANSIT_NET){
                java.net.URL u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/transitNetBlu.gif");
                this.bufferedImageNodoBlu= ImageIO.read(u);
                u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/transitNetRosso.gif");
                this.bufferedImageNodoRosso= ImageIO.read(u);
            }
            //carica i nodi come nodi transit
            if(image==CirclePanel.TRANSIT_NODE){
                java.net.URL u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/transitNodeBlu.gif");
                this.bufferedImageNodoBlu= ImageIO.read(u);
                u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/transitNodeRosso.gif");
                this.bufferedImageNodoRosso= ImageIO.read(u);
            }
        }
        catch(java.lang.Exception e){
        }
    }
        
    
    private void mouseClick(java.awt.event.MouseEvent evt){
        this.selectedLink=-1;
        this.setToolTipText(null);
        this.repaint();
    }
   
    
    public void showNetwork(int id_){
        this.selectedLink=id_;
        this.setToolTipText("Click here to clear");
        this.repaint();
    }
    
    
     public void paintComponent(Graphics g) {
       Rectangle r=new Rectangle(this.getBounds());
       g.clearRect(0,0,r.width ,r.height);
       g.setColor(Color.BLACK);
       //int centroX= (r.x+r.width)/2;
       //int centroY=(r.y+r.height)/2;
       //calcolo i nodi da colorare
       for (int i=0;i<this.numNode;i++){
            this.redNode[i]=false;
        }
       if (this.selectedLink>=0){
           this.redNode[this.selectedLink]=true;
           for(int i=0;i<this.numNode;i++){
               if(this.link[this.selectedLink][i]>0 ||
               this.link[i][this.selectedLink]>0){
                   this.redNode[i]=true;
               }
           }
       }
      //Disegno i link
       for(int i=0;i<this.numNode;i++){ 
           for(int j=0;j<this.numNode;j++){
               if (i==this.selectedLink || j==this.selectedLink){
                   g.setColor(Color.RED);
               }
               else{
                   g.setColor(Color.BLACK);
               }
               if (this.link[i][j]!=0){
                   double angolo1=(6.28/this.numNode)*i+4.71;//deb
                   double angolo2=(6.28/this.numNode)*j+4.71;//deb
                   int x1=(int)(Math.cos(angolo1)*(r.width*0.35)+(r.width/2));
                   int y1=(int)(Math.sin(angolo1)*(r.height*0.35)+(r.height/2));
                   int x2=(int)(Math.cos(angolo2)*(r.width*0.35)+(r.width/2));
                   int y2=(int)(Math.sin(angolo2)*(r.height*0.35)+(r.height/2));
                   g.drawLine(x1,y1,x2,y2);
               }
           }
       }
       //Disegno i Nodi
       for (int i=0;i<this.numNode;i++){
           double angolo=(6.28/this.numNode)*i+4.71;//deb
           int x=(int)(Math.cos(angolo)*(r.width*0.35)+(r.width/2));
           int y=(int)(Math.sin(angolo)*(r.height*0.35)+(r.height/2));
           if (this.redNode[i]){
               try{
                   Graphics2D g2 = (Graphics2D) g;
                   g2.drawImage(this.bufferedImageNodoRosso,null,x-18,y-13 );
               }
               catch(java.lang.Exception e){
                   g.setColor(Color.RED);
                   int raggio=5;
                   g.fillOval(x-raggio, y-raggio, 2*raggio,2*raggio);
               }
           }
           else{
               try{
                   Graphics2D g2 = (Graphics2D) g;                   
                   g2.drawImage(this.bufferedImageNodoBlu,null,x-18,y-13 );
               }
               catch(java.lang.Exception e){
                   g.setColor(Color.BLACK);
                   int raggio=5;
                   g.fillOval(x-raggio, y-raggio, 2*raggio,2*raggio);
               }
           }
           g.setColor(Color.BLACK);
       }
       //Nomino i Nodi
       for (int i=0;i<this.numNode;i++){
           double angolo=(6.28/this.numNode)*i+4.71;//deb
           int x=(int)(Math.cos(angolo)*(r.width*0.42)+(r.width/2));
           int y=(int)(Math.sin(angolo)*(r.height*0.42)+(r.height/2));
           if (this.redNode[i]){
               g.setColor(Color.RED);
           }
           else{
               g.setColor(Color.BLACK);
           }
           g.drawString(this.nomiNodi[i], x, y);
           g.setColor(Color.BLACK);
       }     
  }//fine paintComponent
     
}//fine classe

